package sys.spvisor.console.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import sys.spvisor.core.entity.project.TBigProject;
import sys.spvisor.core.entity.project.TFileForm;
import sys.spvisor.core.entity.project.TProject;
import sys.spvisor.core.entity.project.TProjectCustom;
import sys.spvisor.core.entity.project.TProjectPeople;
import sys.spvisor.core.entity.project.TSendCard;
import sys.spvisor.core.service.people.PeopleService;
import sys.spvisor.core.service.project.ProjectService;


@Controller
@RequestMapping("/project")
public class ProjectController {

	@Autowired
	ProjectService projectService;
	
	@Autowired
	PeopleService peopleService;
	
	@RequestMapping("/addProject.ajax")
	public @ResponseBody Map<String, Object> addProject(TProjectCustom custom,MultipartHttpServletRequest request, HttpSession session, HttpServletRequest req) {
		String username = (String) session.getAttribute("USER_NAME");
		Long userId =  (Long) session.getAttribute("USER_ID");
		Map<String, Object> result = new HashMap<String, Object>(2);
		Map<String, MultipartFile> fileMap 	= request.getFileMap();
		String msg = "";
		try {
			msg = projectService.addProject(custom,fileMap,username,userId);
			result.put("success", true);
			result.put("message", "新增项目成功！");
		} catch (Exception e) {
			if(msg == "批量导入EXCEL失败！") {
				result.put("success", false);
				result.put("message", "新增项目失败！导入物资清单出现了问题");
			} else {
				result.put("success", false);
				result.put("message", "新增项目失败！");
			}
		}
		
		return result;
		
	}
	
	//修改委托信息
	@RequestMapping("/modifyEntrusting.ajax")
	public @ResponseBody Map<String, Object> modifyEntrusting(Integer proId, TProjectCustom custom,MultipartHttpServletRequest request, HttpSession session, HttpServletRequest req) {
		String username = (String) session.getAttribute("USER_NAME");
		Long userId =  (Long) session.getAttribute("USER_ID");
		custom.gettProject().setProId(proId);
		System.out.println(custom.gettProject().getProId());
		
		Map<String, Object> result = new HashMap<String, Object>(2);
		Map<String, MultipartFile> fileMap 	= request.getFileMap();
		try {
			projectService.modifyEntrustingByMsgId(custom,fileMap,username,userId);
			result.put("success", true);
			result.put("message", "修改委托信息成功！");
		} catch (Exception e) {
			result.put("success", false);
			result.put("message", "修改委托信息失败！");
		}
		
		return result;
		
	}
	
	
	@RequestMapping("/queryProject.ajax")
	public @ResponseBody Map<String, Object> queryProject(TProject msg) {
		Map<String, Object> result = new HashMap<String, Object>(3);
		List<TProject> lists = null;
		try {
			lists = projectService.selectByExample(msg);
			int count = lists.size();
			result.put("total", count);
			result.put("rows", lists);
			result.put("success", true);
			result.put("message", "查询成功");
		} catch (Exception e) {
			result.put("rows", null);
			result.put("success", false);
			result.put("message", "没有查到相关项目，请重新输入");
		}
		return result;
		
	}
	
	
	//根据项目的id，查找项目对应的委托信息以及文件
	@RequestMapping("/getEntrustFileInfo.ajax")
	public @ResponseBody Map<String, Object> getEntrustFileInfo(int proId) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		List<TFileForm> tFileForm = projectService.selectEntrustFileByProjectId(proId);
		
		result.put("tFileForm", tFileForm);
		result.put("length", tFileForm.size());

		return result;
		
	}
	
	@ResponseBody
	@RequestMapping(value="/getProjectName.ajax", method = { RequestMethod.POST })
	public  String getProjectName(Integer projectId) {
		return projectService.getEncodeProjectNameBymsgId(projectId);
	}
	

	
	//根据业主单位查询委托信息
	@ResponseBody
	@RequestMapping(value="/viewProjectByUnitName.ajax", method = { RequestMethod.POST })
	public TProject viewProjectByUnitName(@RequestParam("unitName") String unitName) {
		return projectService.viewProjectByUnitName(unitName);
		
	}
	
	//根据项目id查找子项目详细信息
	@ResponseBody
	@RequestMapping(value="/viewProject.ajax", method = { RequestMethod.POST })
	public  TProject viewProject(@RequestParam("proId")int proId) {
	
		System.out.println("testview");
		return projectService.selectByPrimaryKey(proId);
		
	}
	
	//获取所有的大项目名称
	@ResponseBody
	@RequestMapping("/getAllBigProjectName.ajax")
	public List<TBigProject> getAllBigProjectName() {
		return projectService.getAllBigProjectName();
	}

	//根据项目id查询参与项目的相关人员
		@ResponseBody
		@RequestMapping("/getAllItemPeopleByProId.ajax")
		public Map<String, Object> getAllItemPeopleByProId (@RequestParam("proId")int proId){
			List<TProjectPeople> lists = projectService.getAllItemPeopleByProId(proId);
			Map<String, Object> result = new HashMap<String, Object>();
			String memberNames = "";
			List<TProjectPeople> lists1 = new ArrayList<TProjectPeople>();
			for (TProjectPeople tProjectPeople : lists) {
				lists1.add(tProjectPeople);
			}

			for (TProjectPeople tProjectPeople : lists) {
				if(tProjectPeople.getPpProRoleDesc().equals("监理项目经理") || tProjectPeople.getPpProRoleDesc() == "监理项目经理") {
					result.put("manager", tProjectPeople);
					int userId = tProjectPeople.getPpPeopleid();
					String conntel = peopleService.getPeopleById(userId).getPeoContel();
					result.put("managerTel", conntel);
					lists1.remove(tProjectPeople);
					System.out.println("监理项目经理");
				} else if(tProjectPeople.getPpProRoleDesc().equals("监理项目总监") || tProjectPeople.getPpProRoleDesc() == "监理项目总监") {
					result.put("chief", tProjectPeople);
					int userId = tProjectPeople.getPpPeopleid();
					String conntel = peopleService.getPeopleById(userId).getPeoContel();
					result.put("chiefTel", conntel);
					lists1.remove(tProjectPeople);
					System.out.println("监理项目总监");
				} else if(tProjectPeople.getPpProRoleDesc().equals("监理项目组长") || tProjectPeople.getPpProRoleDesc() == "监理项目组长") {
					result.put("headman", tProjectPeople);
					int userId = tProjectPeople.getPpPeopleid();
					String conntel = peopleService.getPeopleById(userId).getPeoContel();
					result.put("headmanTel", conntel);
					lists1.remove(tProjectPeople);
					System.out.println("监理项目组长");
				}else {
					//拼接组员的名字
					memberNames += tProjectPeople.getPpUsername() + ",";
				}
			}
			//去掉组员名字的最后一个逗号
			String lastName = "";
			if(memberNames != "") {
				lastName = memberNames.substring(0, memberNames.length() - 2);
			}
			result.put("member", lists1);
			result.put("name", lastName);
			return result;
			
		}
		
		//根据项目id查询项目所有的文件信息
		@ResponseBody
		@RequestMapping("/getAllItemFileByProId.ajax")
		public List<TFileForm> getAllItemFileByProId(@RequestParam("proId")int proId) {
			List<TFileForm> lists = projectService.getAllItemFileByProId(proId);
			return lists;
			
		}
		
		//根据项目id查询派遣单信息
			@ResponseBody
			@RequestMapping("/getSendCardFileByProId.ajax")
			public TSendCard getSendCardFileByProId(@RequestParam("proId")int proId) {
				return projectService.getSendCardByProId(proId);
			}
			
		//开工报审修改或者添加
			@ResponseBody
			@RequestMapping("/addOrUpdateBeginWork.ajax")
		public Map<String,Object>	addOrUpdateBeginWork(int proId,String fileNameTotal,MultipartHttpServletRequest request, HttpSession session, HttpServletRequest req) {
			String username = (String) session.getAttribute("USER_NAME");
			Long userId =  (Long) session.getAttribute("USER_ID");
			Map<String, Object> result = new HashMap<String, Object>(2);
			Map<String, MultipartFile> fileMap 	= request.getFileMap();
			try {
				projectService.addOrUpdateBeginWork(proId,fileNameTotal,fileMap,username,userId);
				result.put("success", true);
				result.put("message", "添加/修改开工报审信息成功！");
			} catch (Exception e) {
				result.put("success", false);
				result.put("message", "添加/修改开工报审信息失败！");
			}
			
			return result;
			
		}
			
			@RequestMapping(value="/getBeginWorkByProId.ajax", method = { RequestMethod.POST })
			public @ResponseBody Map<String, Object> getBeginWorkByProId(int proId) {
				
				Map<String, Object> result = new HashMap<String, Object>();
				
				List<TFileForm> tFileForm = projectService.getBeginWorkByProId(proId);
				
				result.put("tFileForm", tFileForm);
				result.put("length", tFileForm.size());

				return result;
				
					
			}
			@ResponseBody
			@RequestMapping(value="/initBeginWorkByProId.ajax", method = { RequestMethod.POST })
			public Map<String, Object> initBeginWorkByProId(int proId) {
				Map<String,Object> result = new HashMap<String,Object>();
				String projectName = projectService.getProjectNameBymsgId(proId);
				result.put("projectName", projectName);
				 List<TFileForm> lists =  projectService.getBeginWorkByProId(proId);
				result.put("tFileForm", lists);
				return result;
					
			}
}
